package dk.modified.reshary;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class ShowOwnerSharesAdapter extends ArrayAdapter<Owners> {

	private Picasso picasso;
	private Context c;
	private int layoutResourceId;
	private ArrayList<Owners> data = null;

	public ShowOwnerSharesAdapter(Context context, int layoutResourceId,
			ArrayList<Owners> data) {
		super(context, layoutResourceId, data);
		this.c = context;
		this.data = data;
		this.layoutResourceId = layoutResourceId;
		picasso = PabloPicasso.with(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderItem viewHolder;

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) c).getLayoutInflater();
			convertView = inflater.inflate(layoutResourceId, parent, false);

			viewHolder = new ViewHolderItem();
			viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
			viewHolder.txtshares = (TextView) convertView
					.findViewById(R.id.shares);
			viewHolder.cmd_video_play = (ImageView) convertView
					.findViewById(R.id.cmd_video_play);
			viewHolder.txt_title = (TextView) convertView
					.findViewById(R.id.txt_title);
			viewHolder.cmd_share = (ImageButton) convertView
					.findViewById(R.id.cmd_share);
			viewHolder.cmd_comment = (ImageButton) convertView
					.findViewById(R.id.cmd_comment);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolderItem) convertView.getTag();
		}

		final Owners owner = data.get(position);
		if (owner != null) {
			picasso.load(owner.getFilename()).into(viewHolder.image);
			if (owner.getMediaState() == Owners.STATE_GIF) {
				viewHolder.cmd_video_play.setVisibility(View.VISIBLE);
			} else {
				viewHolder.cmd_video_play.setVisibility(View.GONE);
			}
			viewHolder.txtshares.setText(owner.getShares() + " ");
			viewHolder.txt_title.setText(owner.getTitle());
			viewHolder.cmd_share.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					openSharingIntent(owner);
				}
			});
			viewHolder.cmd_comment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					
					Intent startCommentActivity = new Intent(c,
							CommentActivity.class);
					startCommentActivity.putExtra("mediaid",Integer.parseInt(owner.getId()));
					c.startActivity(startCommentActivity);
					((Activity) c).overridePendingTransition(R.anim.fadeout, R.anim.fadein);
				}
			});
		}

		return convertView;
	}

	static class ViewHolderItem {
		ImageView image;
		TextView txtshares;
		TextView txt_title;
		ImageView cmd_video_play;
		ImageView cmd_share;
		ImageView cmd_comment;
	}

	private void openSharingIntent(Owners owner) {
		Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
				"Reshary image");
		String shareMessage = c.getString(R.string.share_media_text) + " "
				+ owner.getSharingurl();
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);

		c.startActivity(Intent.createChooser(sharingIntent, "Share"));
	}

}
